package cn.buk.qms.service;

import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.dao.EnterpriseDao;
import cn.buk.tms.dao.FlightOrderDao;
import cn.buk.tms.dto.PassengerRecency;
import cn.buk.tms.entity.order.flight.FlightOrder;
import cn.buk.tms.entity.order.flight.FlightOrderPassenger;
import cn.buk.tms.common.constant.TmsConstant;
import cn.buk.common.util.DateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yfdai
 */
@Service
public class EnterpriseStatServiceImpl implements EnterpriseStatService {

  private static final Logger logger = LogManager.getLogger(EnterpriseStatServiceImpl.class);

  private final EnterpriseDao enterpriseDao;
  private final FlightOrderDao flightOrderDao;

  public EnterpriseStatServiceImpl(EnterpriseDao enterpriseDao,
                                   FlightOrderDao flightOrderDao) {
    this.enterpriseDao = enterpriseDao;
    this.flightOrderDao = flightOrderDao;
  }

  @Override
  public void refreshPassengerRecency() {

    final long start = System.currentTimeMillis();

    CommonSearchCriteria sc = new CommonSearchCriteria();
    sc.setPageSize(1000);

    final Date startDate = DateUtil.addDays(DateUtil.getCurDate(), -365);
    Map<String, PassengerRecency> map = new HashMap<>();
    do {
      List<FlightOrder> flightOrders = flightOrderDao.searchOrder(sc, startDate);
      logger.info(flightOrders.size() + ", " + sc.getPageNo() + "/" + sc.getPage().getPageTotal());
      for(var flightOrder: flightOrders) {
        processPassengerRecencyFlightOrder(flightOrder, map);
      }

      sc.setPageNo(sc.getPageNo() + 1);
    } while (sc.getPageNo() <= sc.getPage().getPageTotal());

    for(var m: map.values()) {
      enterpriseDao.refreshPassengerRecency(m);
    }

    long elapsed = System.currentTimeMillis() - start;
    logger.info("refreshPassengerRecency: " + elapsed + "ms.");
  }

  private void processPassengerRecencyFlightOrder(FlightOrder flightOrder, Map<String, PassengerRecency> map) {
    if (flightOrder.getCustomer() == null) {
      //散客不统计
      return;
    }
    for(FlightOrderPassenger psg: flightOrder.getPassengers()) {
      //暂时只统计身份证的
      if (psg.getIdType() != TmsConstant.ID_TYPE_NI) {
        continue;
      }
      final String key = psg.getIdNo().trim();
      PassengerRecency pr = map.get(key);
      if (pr == null) {
        pr = new PassengerRecency();
        pr.setEnterpriseId(flightOrder.getEnterpriseId());
        pr.setCustomerId(flightOrder.getCustomer() == null ? 0 : flightOrder.getCustomer().getId());
        pr.setName(psg.getName());
        pr.setIdno(psg.getIdNo());
        pr.setBuyCount(1);
        pr.setRecency(flightOrder.getCreateTime());
        map.put(key, pr);
      } else {
        pr.setBuyCount(pr.getBuyCount()+1);
        if (pr.getRecency().getTime() < flightOrder.getCreateTime().getTime()) {
          pr.setRecency(flightOrder.getCreateTime());
        }
      }
    }
  }
}
